package com.unittec.zk.provider.work.controller;

import com.alibaba.fastjson.JSON;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.utils.ValidationUtils;
import com.unittec.zk.provider.work.service.CarInfoService;
import com.unittec.zk.sdk.exam.response.Res201000;
import com.unittec.zk.sdk.log.enums.LogOperateEnum;
import com.unittec.zk.sdk.log.enums.LogStatusEnum;
import com.unittec.zk.sdk.root.domain.GatewayRequest;
import com.unittec.zk.sdk.root.domain.Result;
import com.unittec.zk.sdk.root.enums.BizCode;
import com.unittec.zk.sdk.root.enums.BizCodeEnum;
import com.unittec.zk.sdk.work.request.Req202001;
import com.unittec.zk.sdk.work.request.Req202002;
import com.unittec.zk.sdk.work.request.Req202003;
import com.unittec.zk.sdk.work.response.Res202000;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(tags = "车辆管理")
public class CarManagerController {

    @Resource
    private Log log;

    @Resource
    private CarInfoService carInfoService;

    //车辆信息-分页
    @RequestMapping(value = "/202001", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "车辆信息-分页", notes = "车辆信息-分页")
    public Result<Res202000> getCarPage(@RequestBody GatewayRequest<Req202001> req) {
        log.debug(req, "getCarPage {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res202000 res202000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = carInfoService.getCarPage(req.getContent());
            res202000 = new Res202000();
            res202000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("202001", JSON.toJSONString(req), "车辆信息-分页", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getCarPage {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("202001", JSON.toJSONString(req), "车辆信息-分页", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res202000);
        } else {
            log.addOperateLog("202001", JSON.toJSONString(req), "车辆信息-分页", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //车辆信息-增/改
    @RequestMapping(value = "/202002", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "车辆信息-增/改", notes = "车辆信息-增/改")
    public Result<Res202000> carSaveOrUpdate(@RequestBody GatewayRequest<Req202002> req) {
        log.debug(req, "carSaveOrUpdate {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res202000 res202000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = carInfoService.carSaveOrUpdate(req.getContent());
            res202000 = new Res202000();
            res202000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("202002", JSON.toJSONString(req), "车辆信息-增/改", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "carSaveOrUpdate {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("202002", JSON.toJSONString(req), "车辆信息-增/改", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res202000);
        } else {
            log.addOperateLog("202002", JSON.toJSONString(req), "车辆信息-增/改", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //车辆信息-逻辑删
    @RequestMapping(value = "/202003", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "车辆信息-逻辑删", notes = "车辆信息-逻辑删")
    public Result<Res202000> carInfoDelbyIds(@RequestBody GatewayRequest<Req202003> req) {
        log.debug(req, "carInfoDelbyIds {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res202000 res202000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = carInfoService.carInfoDelbyIds(req.getContent());
            res202000 = new Res202000();
            res202000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("202003", JSON.toJSONString(req), "车辆信息-逻辑删", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "carInfoDelbyIds {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("202003", JSON.toJSONString(req), "车辆信息-逻辑删", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res202000);
        } else {
            log.addOperateLog("202003", JSON.toJSONString(req), "车辆信息-逻辑删", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
}
